<?php

namespace app\model;

use think\exception\ValidateException;
use think\Model;

class TrademarkOrder extends Model
{

    protected $connection = 'mysql';

    protected $pk = 'id';
    protected $name = 'trademark_order';
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    public function member()
    {
        return $this->hasOne(Member::class, 'id', 'uid');
    }

    public function paymethod()
    {
        return $this->hasOne(Paymethod::class, 'id', 'pay_method_id');
    }

    public function trademark()
    {
        return $this->hasOne(Trademark::class, 'id', 'trademark_id');
    }

    //订单支付成功
    public function pay_order($order_info)
    {
            $cashlogsdata['uid']             = $order_info['uid'];
            $cashlogsdata['weid']            = weid();
            $cashlogsdata['order_id']        = $order_info['id'];
            $cashlogsdata['order_num_alias'] = $order_info['order_num_alias'];
            $cashlogsdata['remarks']         = '购买商标消费';
            $cashlogsdata['type']            = 2;
            $cashlogsdata['prefix']          = 2;
            $cashlogsdata['amount']          = $order_info['total'];
            $cashlogsdata['create_time']     = $order_info['pay_time'];
            MemberCashlogs::create($cashlogsdata);
            //分销佣金处理
            $agent = Config::getconfig('agent');
            $share = Config::getconfig('share');
            if (!empty($agent['level'])) {
                $level = $agent['level'];
            }
            if (!empty($agent['is_rebate'])) {
                $is_rebate = $agent['is_rebate'];
            }
            //一层佣金
            if (!empty($level)) {
                if ($level > 0) {
                    $firstpercent = $share['first'];
                    if (!empty($firstpercent)) {
                        $firstmember = Member::find($order_info['uid']);

                        if (!empty($firstmember)) {
                            $firstmember             = $firstmember->toArray();
                            $firstmember['is_agent'] = Agent::is_agent($firstmember['id']);
                        }
                        if (!empty($is_rebate) && $firstmember['is_agent'] == 1) {
                            $firstuid = $order_info['uid'];
                        } else {

                            $firstuid = $firstmember['pid'];
                        }

                        Agent::setIncome($firstuid, $order_info, $firstpercent, 1);
                    }
                }
                //二层佣金
                if ($level > 1 && !empty($firstuid)) {
                    $secondpercent = $share['second'];
                    if (!empty($secondpercent)) {
                        $secondmember = Member::find($firstuid);
                        if (!empty($secondmember)) {
                            $secondmember = $secondmember->toArray();
                        }
                        if ((int)$secondmember['pid'] > 0) {
                            Agent::setIncome($secondmember['pid'], $order_info, $secondpercent, 2);
                        }
                    }
                }

                //三层佣金
                if ($level > 2 && !empty($secondmember['pid'])) {
                    $thirdpercent = $share['third'];
                    if (!empty($thirdpercent)) {
                        $thirdmember = Member::find($secondmember['pid']);
                        if (!empty($thirdmember)) {
                            $thirdmember = $thirdmember->toArray();
                        }
                        if ((int)$thirdmember['pid'] > 0) {
                            Agent::setIncome($thirdmember['pid'], $order_info, $thirdpercent, 3);
                        }
                    }
                }
            }
    }

    public function orderStatus()
    {
        return $this->hasOne(OrderStatus::class, 'id', 'order_status_id');
    }

    //订单信息
    public static function order_info($id)
    {

        $order = self::where('id', $id)->with(['member', 'orderStatus', 'trademark', 'paymethod'])->find();

        if (!empty($order)) {
            $order = $order->toArray();
        }
        if (!$order) {
            return false;
        }

        $order['shipping_province'] = $order['shipping_province_name'];
        $order['shipping_city']     = $order['shipping_city_name'];
        $order['shipping_district'] = $order['shipping_district_name'];
        $order['shipping_district'] = $order['shipping_district_name'];
        $order['shipping_district'] = $order['shipping_district_name'];
        $order['pay_time']    = time_format($order['pay_time']);
        $order['create_time'] = time_format($order['create_time']);
        $order['update_time'] = time_format($order['update_time']);
        $order['goods'] = Trademark::where(['id'=>$order['trademark_id']])->select();
        return array (
            'orderInfo'      => $order
        );
    }
}
